<?php

    // +----------------------------------------------------------------------
    // | 文档模型
    // +----------------------------------------------------------------------
    // | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
    // +----------------------------------------------------------------------
    // | Author: 微尘 <yicmf@qq.com>
    // +----------------------------------------------------------------------

    namespace app\model\cms;

    use app\model\file\Image;
    use app\model\ucenter\User;

    /**
     * 文档基础模型.
     */
    class Document extends Common
    {

        //关联模型
        public function user()
        {
            return $this->belongsTo(User::class);
        }

        /**
         * 图片模型
         */
        public function photo()
        {
            return $this->hasOne(DocumentPhoto::class);
        }

        /**
         * 数据模型
         */
        public function pattern()
        {
            return $this->belongsTo(Pattern::class);
        }
		public function getCoverIdAttr($value)
		{
			return $value ?? 119;
		}

        /**
         * 下载模型
         */
        public function download()
        {
            return $this->hasOne(DocumentNotice::class);//->bind(Attribute::getNamesByModel(3));
        }

        public function notice()
        {
            return $this->hasOne(DocumentNotice::class);
        }

        /**
         * 普通文档模型
         */
        public function article()
        {
            return $this->hasOne(DocumentArticle::class);//->bind(Attribute::getNamesByModel(2));
        }

        /**
         * 标签
         */
        public function tags()
        {
            return $this->hasMany('TagsContent', 'document_id', 'id');
        }

        /**
         * 封面
         */
        public function cover()
        {
            return $this->belongsTo(Image::class, 'cover_id');
        }

        /**
         * 来源
         */
        public function source()
        {
            return $this->belongsTo(Source::class);
        }

        /**
         * 分类
         */
        public function category()
        {
            return $this->belongsTo(Category::class);
        }

        protected $type = [
        ];
		/**
		 * 写入修改
		 * @param $data
		 */
		public static function onBeforeInsert($data)
		{
			$data['status'] = isset($data['status'])?$data['status']:1;
			$data['start_time'] = time_format();
		}


    }
